import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB

# 加载数据
input_file = 'F:/python学习资料/Python-Machine-Learning-Cookbook-master/Chapter02/data_multivar.txt'

X = []
y = []
with open(input_file,'r') as f:
    for line in f.readlines():
        data = [float(x) for x in line.split(',')]
        X.append(data[:-1])
        y.append(data[-1])

X = np.array(X)
y = np.array(y)

classifier_gaussiannb = GaussianNB()
classifier_gaussiannb.fit(X,y)
y_pred = classifier_gaussiannb.predict(X)

accuracy = 100*(y==y_pred).sum() / X.shape[0]
print("Accuracy of the classifier =",round(accuracy,2),'%')

# 画出数据点和边界
x_min,x_max = min(X[:,0])-1,max(X[:,1])+1
y_min,y_max = min(X[:,1])-1,max(X[:,1])+1

# 设置网格数据的步长
step_size = 0.01
# 定义网格
x_values,y_values = np.meshgrid(np.arange(x_min,x_max,step_size),np.arange(x_min,x_max))
# 计算分类器结果
mesh_output = classifier_gaussiannb.predict(np.c_[x_values.ravel(),y_values.ravel()])
# 数组变形
mesh_output = mesh_output.reshape(x_values.shape)

# 将分类结果可视化
plt.figure()
# 选择配色方案
plt.pcolormesh(x_values,y_values,mesh_output,cmap=plt.cm.gray)
# 在图中画出训练数据点
plt.scatter(X[:,0],X[:,1],c=y,s=80,edgecolors='black',linewidth=1,cmap=plt.cm.Paired)

# 设定图形边界
plt.xlim(x_values.min(),x_values.max())
plt.ylim(y_values.min(),y_values.max())

# # 设定x轴和y轴的刻度
# plt.xticks((np.arange(int(min(X[:,0])-1),int(max(X[:,0])+1),1)))
# plt.yticks((np.arange(int(min(X[:,1])-1,int(max[:,1])+1),1)))

plt.show()

# 用交叉验证评估模型准确度
from sklearn import model_selection
num_validations = 5
accuracy = model_selection.cross_val_score(classifier_gaussiannb,X,y,cv=num_validations,scoring='accuracy')
print("Accuracy:"+str(round(100*accuracy.mean(),2))+'%')

# 用前面的函数计算查准率,查全率和F1分数
# f1分数
f1 = model_selection.cross_val_score(classifier_gaussiannb,X,y,scoring='f1_weighted',cv=num_validations)
print('F1:'+str(round(100*f1.mean(),2))+'%')

# precision 查准率
precision = model_selection.cross_val_score(classifier_gaussiannb,X,y,scoring='precision_weighted',cv=num_validations)
print("Precision:"+str(100*round(precision.mean(),2))+"%")

# recall 召回率（查全率）
recall = model_selection.cross_val_score(classifier_gaussiannb,X,y,scoring='recall_weighted',cv=num_validations)
print("recall:"+str(100*round(recall.mean(),2))+"%")